package com.lovely602.boot.bootdemo.domain.common;

import java.util.HashMap;
import java.util.Map;

import com.lovely602.boot.bootdemo.domain.common.exception.BusinessException;
import com.lovely602.boot.bootdemo.domain.common.exception.ErrorCodes;
import com.lovely602.boot.bootdemo.domain.common.exception.ErrorLevel;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
public class RestResponse<T> {
  @ApiModelProperty(value = "组件id", required = true, example = "demo")
  private String componentId = "demo";
  @ApiModelProperty(value = "错误级别", example = "B")
  private ErrorLevel errorLevel;

  @ApiModelProperty(value = "响应码", required = true, example = "2000")
  private int code = 2000;
  @ApiModelProperty(value = "结果信息", required = true, example = "接口调用成功")
  private String msg;
  @ApiModelProperty(value = "响应数据", required = true)
  private T data;
  @ApiModelProperty(value = "总数", example = "99")
  private long total = 0L;
  @ApiModelProperty(value = "是否还有更多", example = "true")
  private Boolean more;
  @ApiModelProperty(value = "请求是否成功", example = "true")
  public boolean success = true;

  @ApiModelProperty(value = "提示信息")
  private Map<String, String> fields = new HashMap<>();

  public RestResponse(T data) {
    RestResponse<T> response = new RestResponse<>();
    response.setSuccess(true);
    response.setData(data);
  }

  public static RestResponse<Void> fail(ErrorCodes errorCodes, Object... arguments) {
    RestResponse<Void> response = new RestResponse<>();
    response.setSuccess(false);
    response.setCode(errorCodes.getCode());
    response.setMsg(errorCodes.formatMsg(arguments));
    response.setErrorLevel(errorCodes.getErrorLevel());
    return response;
  }

  public static RestResponse<Void> fail(BusinessException zlException) {
    RestResponse<Void> response = new RestResponse<>();
    response.setSuccess(false);
    response.setCode(zlException.getErrorCodes().getCode());
    response.setMsg(zlException.getMessage());
    response.setErrorLevel(zlException.getErrorCodes().getErrorLevel());
    return response;
  }

  public static RestResponse<Void> success() {
    RestResponse<Void> response = new RestResponse<>();
    response.setSuccess(true);
    return response;
  }

  public static <T> RestResponse<T> success(T data) {
    RestResponse<T> response = new RestResponse<>();
    response.setSuccess(true);
    response.setData(data);
    return response;
  }

  public static <T> RestResponse<T> success(T data, long total, Boolean more) {
    RestResponse<T> response = new RestResponse<>();
    response.setData(data);
    response.setSuccess(true);
    response.setTotal(total);
    response.setMore(more);
    return response;
  }

  public static <T> RestResponse<T> success(T data, long total, int page, int pageCount) {
    RestResponse<T> response = new RestResponse<>();
    response.setData(data);
    response.setSuccess(true);
    response.setTotal(total);
    response.setMore(page < pageCount - 1);
    return response;
  }
}
